﻿using UnityEngine;
using UnityEngine.UI;

public class UseToolbar : MonoBehaviour {

    public ToolbarAsset toolbarAsset;
    public RectTransform toolbarPanel;

	// Use this for initialization
	void Start () {
        if (null == toolbarAsset)
        {
            return;
        }

        Button prefab = null;
        ToolbarButtonObject item = null;
        int count = toolbarAsset.toolbarButtons.Count;
        for (int i = 0; i < count; i++)
        {
            item = toolbarAsset.toolbarButtons[i];
            Debug.LogFormat("Create Button {0}", item.name);

            switch (item.type)
            {
                case ToolbarButtonObject.IconType.Brands:
                    prefab = toolbarAsset.BrandsButtonPrefab;
                    break;
                case ToolbarButtonObject.IconType.Regular:
                    prefab = toolbarAsset.RegularButtonPrefab;
                    break;
                case ToolbarButtonObject.IconType.Solid:
                    prefab = toolbarAsset.SolidButtonPrefab;
                    break;
            }

            CreateButton(toolbarPanel, item, prefab);
        }
	}

    private void CreateButton(RectTransform toolbarPanel, ToolbarButtonObject item, Button prefab)
    {
        Button button = Instantiate(prefab);
        //item.icon;
        //item.iconSize;
        //item.color;

        Text icon = button.transform.Find("Text").GetComponent<Text>();
        if (null == icon)
        {
            return;
        }

        RectTransform rtButton = button.GetComponent<RectTransform>();
        rtButton.SetParent(toolbarPanel);

        icon.text = item.icon;
        if (item.iconSize > 0)
        {
            icon.fontSize = item.iconSize;
        }

        icon.color = item.color;

        button.gameObject.name = string.Format("Button_{0}", item.name);
    }

    // Update is called once per frame
    void Update () {
		
	}
}
